﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TopShooter.Editor
{
	public partial class ObjectEditor : Form
	{

		static public ObjectEditor Instance;
		static private Point? s_LastLocation;

		private bool _isActive;
		private bool _isEnterDown;

		public Tile EditedTile;

		static public void EditObject(Tile t)
		{
			ObjectEditor win = new ObjectEditor(null, t);
			win.Show();
		}

		public ObjectEditor(Action<string> applyMethod, Tile t)
		{
			Instance = this;
			InitializeComponent();
			txtCommand.KeyUp += new KeyEventHandler(txtCommand_KeyUp);
			txtCommand.KeyDown += new KeyEventHandler(txtCommand_KeyDown);
			txtCommand.Text = t.GetDefinition();
			EditedTile = t;
		}

		void txtCommand_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				if (_isEnterDown == false)
					_isEnterDown = true;
				//Apply();
			}
			else if (e.KeyCode == Keys.Escape)
			{
				this.Close();
			}
		}
		void txtCommand_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				if (_isEnterDown == true)
				{
					_isEnterDown = false;
					Apply();
				}
			}
		}
		protected override void OnShown(EventArgs e)
		{
			if (s_LastLocation.HasValue)
				Location = s_LastLocation.Value;

			_isActive = true;
			base.OnShown(e);
			Game.IsKeyboardDisabled = true;
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			Instance = null;
			s_LastLocation = Location;
			_isActive = false;
			base.OnClosing(e);
			Game.IsKeyboardDisabled = false;
		}
		private void btnEnter_Click(object sender, EventArgs e)
		{
			Apply();
		}

		private void Apply()
		{
			_isActive = false;
			Editor.EditorAction a = new Editor.Actions.EditTileAction(EditedTile, txtCommand.Text);
			a.Apply();
			this.Close();

		}


		private void Btn_GotoPortal(object sender, EventArgs e)
		{
			this.Apply();
			Scene.Instance.SaveSceneState();
			Portal p = EditedTile as Portal;
			Game.Instance.LoadNextScene(p.Definition, false);
		}




	}
}
